var totalCount = 0;

var componentCount_0 = 0;
var componentCount_1 = 0;
var componentCount_2 = 0;
var componentCount_3 = 0;
var componentCount_4 = 0;

var headerSeqence = 0;
var leftSeqence = 0;
var rightSeqence = 0;
var bodySeqence = 0;
var footerSeqence = 0;

var validator;

/*
tinyMCE.init({
	theme : "advanced",
	plugins : "fullscreen",
    theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
    theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,search,replace,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,forecolor,backcolor",
    theme_advanced_buttons3 : "hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,fullscreen",

	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	force_p_newlines : false
});
*/

function caculateOrder(){
	totalCount = 0;
    	    
    headerSeqence = 0;
    bodySeqence = 0;
    footerSeqence = 0;
    
    var componentHeaderList = jQuery("#componentList0").find("table[name='fieldsTable']");
    componentHeaderList.each(function(){
    	var componentTypeObj = jQuery(this).find("input[id*='componentType']");
    	var componentSequenceObj = jQuery(this).find("input[id*='sequence']");
    	var componentPositionObj = jQuery(this).find("input[id*='position']");
    	
    	componentTypeObj.attr('name','componentType'+totalCount);
    	componentSequenceObj.attr('name','sequence'+totalCount);
    	componentPositionObj.attr('name','position'+totalCount);

    	componentSequenceObj.val(headerSeqence);
    	
        var componentType = componentTypeObj.val();
        if(componentType=='LogoComponent'){
			var componentImageObj = jQuery(this).find("input[id*='image']");
			
			componentImageObj.attr('name','image'+totalCount);
        }else if(componentType=='NavComponent'){
        }else if(componentType=='HtmlComponent'){
			var componentContentObj = jQuery(this).find("textarea[id*='content']");
			componentContentObj.attr('name','content'+totalCount);
			
			if(componentContentObj.css('display')=='none'){
				componentContentObj.text(KE.html(componentContentObj.attr('id')));
			}
        }else{
            alert('['+componentType+']组件不应该出现在这个区域');
        }
        headerSeqence=headerSeqence+1;
        totalCount=totalCount+1;
    });
    
    var componentLeftList = jQuery("#componentList1").find("table[name='fieldsTable']");
    componentLeftList.each(function(){
        var componentTypeObj = jQuery(this).find("input[id*='componentType']");
        var componentSequenceObj = jQuery(this).find("input[id*='sequence']");
        var positionObj = jQuery(this).find("input[id*='position']");
        
        componentTypeObj.attr('name','componentType'+totalCount);
        componentSequenceObj.attr('name','sequence'+totalCount);
        positionObj.attr('name','position'+totalCount);

        componentSequenceObj.val(leftSeqence);
        
        var componentType = jQuery(this).find("input[id*='componentType']").val();
        if(componentType=='CatalogComponent'){
        	var componentTitleObj = jQuery(this).find("input[id*='title']");
        	componentTitleObj.attr('name','title'+totalCount);
        	
        	var componentContentObj = jQuery(this).find("input[id*='content']");
        	componentContentObj.attr('name','content'+totalCount);
        	
        	var componentWidthObj = jQuery(this).find("input[id*='width']");
        	componentWidthObj.attr('name','width'+totalCount);
        	
        	var componentHeightObj = jQuery(this).find("input[id*='height']");
        	componentHeightObj.attr('name','height'+totalCount);
        }else if(componentType=='MapComponent'){
        	updateMap(this,totalCount);
        }else if(componentType=='ArticleListComponent'){
		    var componentTitleObj = jQuery(this).find("input[id*='title']");
		    componentTitleObj.attr('name','title'+totalCount);
			
			var componentContentObj = jQuery(this).find("input[id*='content']");
			componentContentObj.attr('name','content'+totalCount);
        }else if(componentType=='HtmlComponent'){
			var componentContentObj = jQuery(this).find("textarea[id*='content']");
			componentContentObj.attr('name','content'+totalCount);
			
			if(componentContentObj.css('display')=='none'){
				componentContentObj.text(KE.html(componentContentObj.attr('id')));
			}
        }else{
            alert('['+componentType+']组件不应该出现在这个区域');
        }
        leftSeqence=leftSeqence+1;
        totalCount=totalCount+1;
    });
	
    var componentRightList = jQuery("#componentList2").find("table[name='fieldsTable']");
    componentRightList.each(function(){
        var componentTypeObj = jQuery(this).find("input[id*='componentType']");
        var componentSequenceObj = jQuery(this).find("input[id*='sequence']");
        var positionObj = jQuery(this).find("input[id*='position']");
        
        componentTypeObj.attr('name','componentType'+totalCount);
        componentSequenceObj.attr('name','sequence'+totalCount);
        positionObj.attr('name','position'+totalCount);

        componentSequenceObj.val(rightSeqence);
        
        var componentType = jQuery(this).find("input[id*='componentType']").val();
        if(componentType=='CatalogComponent'){
        }else if(componentType=='MapComponent'){
        	updateMap(this,totalCount);
        }else if(componentType=='ArticleListComponent'){
		    var componentTitleObj = jQuery(this).find("input[id*='title']");
		    componentTitleObj.attr('name','title'+totalCount);
			
			var componentContentObj = jQuery(this).find("input[id*='content']");
			componentContentObj.attr('name','content'+totalCount);
        }else if(componentType=='HtmlComponent'){
			var componentContentObj = jQuery(this).find("textarea[id*='content']");
			componentContentObj.attr('name','content'+totalCount);
			
			if(componentContentObj.css('display')=='none'){
				componentContentObj.text(KE.html(componentContentObj.attr('id')));
			}
        }else{
            alert('['+componentType+']组件不应该出现在这个区域');
        }
        rightSeqence=rightSeqence+1;
        totalCount=totalCount+1;
    });
	
    var componentBodyList = jQuery("#componentList4").find("table[name='fieldsTable']");
    componentBodyList.each(function(){
        var componentTypeObj = jQuery(this).find("input[id*='componentType']");
        var componentSequenceObj = jQuery(this).find("input[id*='sequence']");
        var positionObj = jQuery(this).find("input[id*='position']");
        
        componentTypeObj.attr('name','componentType'+totalCount);
        componentSequenceObj.attr('name','sequence'+totalCount);
        positionObj.attr('name','position'+totalCount);

        componentSequenceObj.val(bodySeqence);
        
        var componentType = jQuery(this).find("input[id*='componentType']").val();
        if(componentType=='FlashComponent'){
        	updateFlash(this,totalCount);
        }else if(componentType=='MapComponent'){
        	updateMap(this,totalCount);
        }else if(componentType=='SiteMapComponent'){
        }else if(componentType=='TabsComponent'){
        }else if(componentType=='LicenseComponent'){
        }else if(componentType=='HtmlComponent'){
			var componentContentObj = jQuery(this).find("textarea[id*='content']");
			componentContentObj.attr('name','content'+totalCount);
			
			if(componentContentObj.css('display')=='none'){
				componentContentObj.text(KE.html(componentContentObj.attr('id')));
			}
        }else{
            alert('['+componentType+']组件不应该出现在这个区域');
        }
        bodySeqence=bodySeqence+1;
        totalCount=totalCount+1;
    });
    
    var componentFooterList = jQuery("#componentList3").find("table[name='fieldsTable']");
    componentFooterList.each(function(){
        var componentTypeObj = jQuery(this).find("input[id*='componentType']");
        var componentSequenceObj = jQuery(this).find("input[id*='sequence']");
        var positionObj = jQuery(this).find("input[id*='position']");
        
        componentTypeObj.attr('name','componentType'+totalCount);
        componentSequenceObj.attr('name','sequence'+totalCount);
        positionObj.attr('name','position'+totalCount);

        componentSequenceObj.val(footerSeqence);
    
        var componentType = jQuery(this).find("input[id*='componentType']").val();
        if(componentType=='FriendLinkComponent'){
        }else if(componentType=='CopyRightComponent'){
		}else if(componentType=='AboutComponent'){
        }else if(componentType=='ArticleListComponent'){
		    var componentTitleObj = jQuery(this).find("input[id*='title']");
		    componentTitleObj.attr('name','title'+totalCount);
			
			var componentContentObj = jQuery(this).find("input[id*='content']");
			componentContentObj.attr('name','content'+totalCount);
        }else if(componentType=='QuickNavComponent'){
        	var componentTitleObj = jQuery(this).find("input[id*='title']");
        	componentTitleObj.attr('name','title'+totalCount);
        	
        	var componentContentObj = jQuery(this).find("input[id*='content']");
        	componentContentObj.attr('name','content'+totalCount);
        }else if(componentType=='HtmlComponent'){
			var componentContentObj = jQuery(this).find("textarea[id*='content']");
			componentContentObj.attr('name','content'+totalCount);
			
			if(componentContentObj.css('display')=='none'){
				componentContentObj.text(KE.html(componentContentObj.attr('id')));
			}
        }else{
            alert('['+componentType+']组件不应该出现在这个区域');
        }
        footerSeqence=footerSeqence+1;
        totalCount=totalCount+1;
    });
    jQuery('#componentListLength').val(totalCount); 
}

function removeHTMLArea(){
	jQuery("textarea[id*='content']").each(
			function (){
				/*if (tinyMCE.getInstanceById(jQuery(this).attr('id')))
				{
					tinyMCE.execCommand('mceFocus', false, jQuery(this).attr('id'));
					tinyMCE.execCommand('mceRemoveControl', false,jQuery(this).attr('id'));
				}*/
				try 
				{ 
					KE.remove(jQuery(this).attr('id'));
				}catch (e) 
				{ 
				
				}
			}
	);	
}
function onTextareafocus(element){
	removeHTMLArea();
	//tinyMCE.execCommand('mceAddControl', false, element.id);
	
	var editor_id = element.id;
	KE.init({'id': editor_id,width : '700px',afterCreate : function(id) {
				resetPageHeight();
				KE.util.focus(id);
			}
		}
	);

	KE.create(editor_id);
}

function onTextareaBlur(element){
	//tinyMCE.execCommand('mceFocus', false, element.id);
	//tinyMCE.execCommand('mceRemoveControl', false,element.id);
	//resetPageHeight();
}

function updateFlash(element,totalCount){
	var componentImagesObj = jQuery(element).find("textarea[id*='images']");
	componentImagesObj.attr('name','images'+totalCount);
	
	var componentContentObj = jQuery(element).find("textarea[id*='content']");
	componentContentObj.attr('name','content'+totalCount);
}
function updateMap(element,totalCount){
	var componentTitleObj = jQuery(element).find("input[id*='title']");
	componentTitleObj.attr('name','title'+totalCount);
	
	var componentContentObj = jQuery(element).find("input[id*='content']");
	componentContentObj.attr('name','content'+totalCount);
	
	var componentWidthObj = jQuery(element).find("input[id*='width']");
	componentWidthObj.attr('name','width'+totalCount);
	
	var componentHeightObj = jQuery(element).find("input[id*='height']");
	componentHeightObj.attr('name','height'+totalCount);
}